<?php

use Drupal\shusy_hotel\Entity\ShusyHotel;
use Drupal\shusy_hotel\Entity\ShusyHotelOrder;
use Drupal\shusy_hotel\Entity\ShusyHotelRoom;
use Drupal\shusy_hotel\Entity\ShusyHotelLine;
use Drupal\shusy_hotel\Entity\ShusyHotelLineRoom;

function shusy_hotel_shusy_hotel_entity_form($form, &$form_state, ShusyHotel $e, $op) {
  $form_state['build_info']['files']['form'] = drupal_get_path('module', 'shusy_hotel') . '/shusy_hotel.forms.inc';
  $form_state[ShusyHotel::class] = $e;

  $form['store_id'] = [
    '#type' => 'select',
    '#title' => t('所属店铺'),
    '#options' => shusy_store_options(),
    '#default_value' => $e->store_id,
    '#chosen' => TRUE,
    '#required' => TRUE,
  ];
  $form['name'] = [
    '#type' => 'textfield',
    '#title' => t('酒店名'),
    '#required' => TRUE,
    '#default_value' => $e->name,
  ];
  $form['address'] = [
    '#type' => 'textfield',
    '#title' => t('酒店地址'),
    '#required' => TRUE,
    '#default_value' => $e->address,
  ];

  $longitude = $e->getLongitude();
  $latitude = $e->getLatitude();
  $form['basic_map'] = [
    '#type' => 'fieldset',
    '#title' => t('地图信息'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
    '#prefix' => '<div id="map">',
    '#suffix' => '</div>',
  ];
  $form['basic_map']['search_address'] = [
    '#type' => 'textfield',
    '#title' => t('检索地址'),
    '#size' => 60,
    '#maxlength' => 128,
    '#description' => t('方便在地图中快速标记点.'),
  ];
  $form['basic_map']['search_address']['searchResultPanel'] = [
    '#type' => 'item',
    '#markup' => '<div id="searchResultPanel" style="border:1px solid #C0C0C0;width:150px;height:auto;display: none;"></div>',
  ];
  $form['basic_map']['container'] = [
    '#type' => 'container',
    '#prefix' => '<div id="container" class="map container">',
    '#suffix' => '</div>',
  ];
  $form['basic_map']['coordinate'] = [
    '#type' => 'hidden',
    '#default_value' => $e->coordinate,
  ];
  $form['basic_map']['lng'] = [
    '#type' => 'hidden',
    '#default_value' => $longitude,
  ];
  $form['basic_map']['lat'] = [
    '#type' => 'hidden',
    '#default_value' => $latitude,
  ];

  $form['#attached']['js'][] = 'https://api.map.baidu.com/api?v=3.0&ak=' . variable_get_value('store_baidu_ak');
  $form['#attached']['js'][] = drupal_get_path('module', 'shusy_hotel') . '/js/shusy_hotel.js';
  $form['#attached']['css'][] = drupal_get_path('module', 'shusy_hotel') . '/css/shusy_hotel.css';
  $settings = [
    'lng' => $longitude,
    'lat' => $latitude,
  ];
  $form['#attached']['js'][] = [
    'data' => ['shusyStoreEntityForm' => $settings],
    'type' => 'setting',
  ];

  $form['status'] = [
    '#type' => 'checkbox',
    '#title' => t('启用'),
    '#default_value' => $e->status,
  ];

  field_attach_form($e->entityType(), $e, $form, $form_state);

  $form['actions'] = ['#type' => 'actions'];
  $form['actions']['submit'] = [
    '#type' => 'submit',
    '#value' => t('保存'),
  ];
  return $form;
}

function shusy_hotel_shusy_hotel_entity_form_submit($form, &$form_state) {
  $e = $form_state[ShusyHotel::class];
  if ($e instanceof ShusyHotel) {
    entity_form_submit_build_entity($e->entityType(), $e, $form, $form_state);

    $longitude = $form_state['values']['lng'];
    $latitude = $form_state['values']['lat'];
    $e->longitude = $longitude;
    $e->latitude = $latitude;
    if ($e->coordinate == 5) {
      $ak = variable_get_value('store_baidu_ak_web');
      $sk = variable_get_value('store_baidu_sk_web');
      $uri = '/geoconv/v1/';
      $querystring_arrays = [
        'coords' => "{$longitude},{$latitude}",
        'ak' => $ak,
        'from' => 5,
        'to' => 3,
        'output' => 'json',
      ];
      $querystring = http_build_query($querystring_arrays);
      $sn = md5(urlencode($uri . '?' . $querystring . $sk));
      $url = "https://api.map.baidu.com{$uri}?{$querystring}&sn={$sn}";
      $result = drupal_http_request($url);
      if ($result->code == 200) {
        $data = $result->data;
        $data = drupal_json_decode($data);
        $bzb = $data['result'];
        $bzb = reset($bzb);
        $longitude = $bzb['x'];
        $latitude = $bzb['y'];
        $e->coordinate = 3;
        $e->longitude = $longitude;
        $e->latitude = $latitude;
      }
    }

    $e->save();
  }
}

function shusy_hotel_shusy_hotel_delete_form($form, &$form_state, ShusyHotel $e) {
  $form_state['build_info']['files']['form'] = drupal_get_path('module', 'shusy_hotel') . '/shusy_hotel.forms.inc';
  $form_state[ShusyHotel::class] = $e;
  return confirm_form($form,
    t('Are you sure you want to delete %title?', ['%title' => $e->label()]),
    'admin/content/shusy_hotel/' . $e->id,
    t('This action cannot be undone.'),
    t('Delete'),
    t('Cancel')
  );
}

function shusy_hotel_shusy_hotel_delete_form_submit($form, &$form_state) {
  $e = $form_state[ShusyHotel::class];
  if ($e instanceof ShusyHotel) {
    $e->delete();
  }
  $form_state['redirect'] = 'admin/content/shusy_hotel';
}

function shusy_hotel_shusy_hotel_order_entity_form($form, &$form_state, ShusyHotelOrder $e, $op) {
  $form_state['build_info']['files']['form'] = drupal_get_path('module', 'shusy_hotel') . '/shusy_hotel.forms.inc';
  $form_state[ShusyHotelOrder::class] = $e;

  field_attach_form($e->entityType(), $e, $form, $form_state);

  $form['actions'] = ['#type' => 'actions'];
  $form['actions']['submit'] = [
    '#type' => 'submit',
    '#value' => t('保存'),
  ];
  return $form;
}

function shusy_hotel_shusy_hotel_order_entity_form_submit($form, &$form_state) {
  $e = $form_state[ShusyHotelOrder::class];
  if ($e instanceof ShusyHotelOrder) {
    entity_form_submit_build_entity($e->entityType(), $e, $form, $form_state);
    $e->save();
  }
}

function shusy_hotel_shusy_hotel_order_delete_form($form, &$form_state, ShusyHotelOrder $e) {
  $form_state['build_info']['files']['form'] = drupal_get_path('module', 'shusy_hotel') . '/shusy_hotel.forms.inc';
  $form_state[ShusyHotelOrder::class] = $e;
  return confirm_form($form,
    t('Are you sure you want to delete %title?', ['%title' => $e->label()]),
    'admin/content/shusy_hotel_order/' . $e->id,
    t('This action cannot be undone.'),
    t('Delete'),
    t('Cancel')
  );
}

function shusy_hotel_shusy_hotel_order_delete_form_submit($form, &$form_state) {
  $e = $form_state[ShusyHotelOrder::class];
  if ($e instanceof ShusyHotelOrder) {
    $e->delete();
  }
  $form_state['redirect'] = 'admin/content/shusy_hotel_order';
}

function shusy_hotel_shusy_hotel_room_entity_form($form, &$form_state, ShusyHotelRoom $e, $op) {
  $form_state['build_info']['files']['form'] = drupal_get_path('module', 'shusy_hotel') . '/shusy_hotel.forms.inc';
  $form_state[ShusyHotelRoom::class] = $e;

  $form['hotel_id'] = [
    '#type' => 'select',
    '#title' => t('所属酒店'),
    '#options' => shusy_hotel_options(),
    '#default_value' => $e->hotel_id,
    '#chosen' => TRUE,
    '#required' => TRUE,
  ];
  $form['name'] = [
    '#type' => 'textfield',
    '#title' => t('房间名'),
    '#required' => TRUE,
    '#default_value' => $e->name,
  ];
  $form['amount'] = [
    '#type' => 'textfield',
    '#title' => t('每晚价格'),
    '#required' => TRUE,
    '#default_value' => $e->amount / 100,
    '#element_validate' => ['element_validate_number'],
  ];
  $form['area'] = [
    '#type' => 'textfield',
    '#title' => t('面积'),
    '#required' => TRUE,
    '#default_value' => $e->area,
    '#element_validate' => ['element_validate_number'],
  ];
  $form['bed'] = [
    '#type' => 'textfield',
    '#title' => t('床位情况'),
    '#required' => TRUE,
    '#default_value' => $e->bed,
  ];
  $form['rooms'] = [
    '#type' => 'textfield',
    '#title' => t('房间总数'),
    '#required' => TRUE,
    '#default_value' => $e->rooms,
    '#element_validate' => ['element_validate_integer'],
  ];
  $form['summary'] = [
    '#type' => 'textarea',
    '#title' => t('摘要介绍'),
    '#default_value' => $e->summary,
  ];
  $form['status'] = [
    '#type' => 'checkbox',
    '#title' => t('上架'),
    '#default_value' => $e->status,
  ];

  field_attach_form($e->entityType(), $e, $form, $form_state);

  $form['actions'] = ['#type' => 'actions'];
  $form['actions']['submit'] = [
    '#type' => 'submit',
    '#value' => t('保存'),
  ];
  return $form;
}

function shusy_hotel_shusy_hotel_room_entity_form_submit($form, &$form_state) {
  $e = $form_state[ShusyHotelRoom::class];
  if ($e instanceof ShusyHotelRoom) {
    entity_form_submit_build_entity($e->entityType(), $e, $form, $form_state);
    $e->amount = bcmul($e->amount, 100);
    $e->save();
  }
}

function shusy_hotel_shusy_hotel_room_delete_form($form, &$form_state, ShusyHotelRoom $e) {
  $form_state['build_info']['files']['form'] = drupal_get_path('module', 'shusy_hotel') . '/shusy_hotel.forms.inc';
  $form_state[ShusyHotelRoom::class] = $e;
  return confirm_form($form,
    t('Are you sure you want to delete %title?', ['%title' => $e->label()]),
    'admin/content/shusy_hotel_room/' . $e->id,
    t('This action cannot be undone.'),
    t('Delete'),
    t('Cancel')
  );
}

function shusy_hotel_shusy_hotel_room_delete_form_submit($form, &$form_state) {
  $e = $form_state[ShusyHotelRoom::class];
  if ($e instanceof ShusyHotelRoom) {
    $e->delete();
  }
  $form_state['redirect'] = 'admin/content/shusy_hotel_room';
}
